<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        .phone{
            width: 300px;
            height: 500px;
            border: 1px solid #333;
            margin: 100px auto;
            position: relative;
        }

        .phone .slideBar{
            width: 100%;
            height: 50px;
            background-color: grey;
            position: absolute;
            left: 0;
            top: 60%;
        }
        .ball {
            width: 50px;
            height: 50px;
            background-color: #6ff;
            border-radius: 50%;
            position: absolute;
            left: 0px;
            top: 0;
        
        }
    </style>
</head>
<body>
    <div class="phone">
        <div class="slideBar">
            <div class="ball"></div>
        </div>
    </div>
</body>
<script>

    var ball = document.querySelector(".ball");
    var slideBar = document.querySelector(".slideBar");
    var phone = document.querySelector(".phone");

    var maxLeft = slideBar.clientWidth - ball.clientWidth;

    ball.onmousedown = function(e){
        var e = e || window.event;
        var posX = e.offsetX;
        console.log(posX);

        document.onmousemove = function(e){
            var e = e || window.event;
            var x = e.pageX - phone.offsetLeft - posX;

            x = Math.min(Math.max(0,x),maxLeft);

            ball.style.left = x + "px";

            if(x === maxLeft){
                document.onmousemove = null;
                location.href = "https://www.baidu.com";
            }

        }
        document.onmouseup = function(){
            document.onmousemove = null;

            if(ball.offsetLeft < maxLeft){
                ball.style.left = 0;
            }


        }

    }



</script>
</html>